import {computed, reactive, ref} from "vue";
import {Layer} from "slash-layer";
import form from "./AuthTbUserLoginLogUpdate.vue";
import httpApi from "./httpApi";

/**
 * 视图控制
 */
export const vCtl = reactive({
    addBtn: computed(() => {
        return true;
    }),
    updateBtn: computed(() => {
        return true && checkedRows.value.length == 1;
    }),
    delBtn: computed(() => {
        return true && checkedRows.value.length >= 1;
    })

});
export const initQuery = {};
export const queryForm = ref<any>(JSON.parse(JSON.stringify(initQuery)));
export const tableRef = ref<any>();

export function reloadTable() {
    if (tableRef.value) {
        tableRef.value.reload();
    }

}

/**
 * 选中的数据
 */
export const checkedRows = ref<any>([]);

/**
 * 选中行
 * @param rows
 */
export async function onCheckedRowEvent(rows: any[]) {
    checkedRows.value = rows;
}

/**
 * 枚举字段
 */
export const enabledTextEnum = reactive(
    {
        1: {
            text: "启用",
            style: "info"
        },
        0: {
            text: "禁用",
            style: "error"
        }
    }
);
export const getEnabledTextEnum = (key: number | string) => {
    // @ts-ignore
    return enabledTextEnum[key].text || `--`;
};

export const getEnabledStyleEnum = (key: number | string) => {
    // @ts-ignore
    return enabledTextEnum[key].style || `--`;
};
export const enabledOptions = computed(() => {
    const temp: any = [];
    Object.keys(enabledTextEnum).forEach(function (key: any) {
        temp.push({
            value: parseInt(key),
            label: enabledTextEnum[key].text
        });
    });
    return temp.reverse();
});

/**
 * 更新弹框
 * @param row
 */
export async function doUpdateModal(row: any) {
    if (checkedRows.value.length != 1 && !row) {
        Layer.error("请选择数据条目后操作");
        return;
    }
    if (!row) {
        row = {
            id: checkedRows.value[0]
        };
    }
    await Layer.updateForm({
        title: "编辑",
        content: {
            component: form,
            props: {...row}
        }
    } as any);
}

/**
 * 新增
 * @param row
 */
export async function doSaveModal(row: any) {
    console.log("传递的数据:{}", row);
    await Layer.createForm({
        title: "新增",
        content: {
            component: form,
            props: {}
        }
    } as any);
    await reloadTable();
}

/**
 * 删除
 * @param row
 */
export async function doDeleteModal(row: any) {
    let ids: any = [];
    if (!row) {
        ids = [...checkedRows.value];
    }
    if (row) {
        ids.push(row.id);
    }

    if (ids.length == 0) {
        Layer.error("请选择要删除的数据条目");
        return;
    }
    await Layer.confirm("您确定删除选中记录么");
    await httpApi.doLogicDeleteEntity(ids);
    await reloadTable();
}

/**
 * 查看表单
 * @param row
 */
export async function doReadModal(row: any) {
    if (checkedRows.value.length != 1 && !row) {
        Layer.error("请选择数据条目后操作");
        return;
    }
    if (!row) {
        row = {
            id: checkedRows.value[0]
        };
    }
    await Layer.readForm({
        title: "编辑登录日志",
        content: {
            component: form,
            props: {...row}
        }
    } as any);
    await reloadTable();

}

/**
 * 列表页面列
 */
export const tableColumns = [
    {
        title: "选中",
        key: "id",
        type: "selection",
        fixed: "left",
        width: 50,
        disabled(row: any) {
            return row.name === "Edward King 3";
        }
    },
    {
        title: "登录名",
        key: "loginName",
        sorter: "loginName",
        searchType:'like',
        width: 150,
        searchProps: {
            type: 'text'
        }
    },
    {
        title: "登录ip",
        key: "loginIp",
        sorter: "loginIp",
        searchType:'like',
        width: 150,
        searchProps: {
            type: 'text'
          }
    },
    {
        title: "登录消息",
        key: "loginMsg",
        sorter: "loginMsg",
        width: 150
    },
    {
        title: "用户编号",
        key: "fkUserId",
        sorter: "fkUserId",
        width: 150
    },
    {
        title: "浏览器",
        key: "loginBrowser",
        sorter: "loginBrowser",
        width: 150
    },
    {
        title: "操作系统",
        key: "loginOs",
        sorter: "loginOs",
        width: 150
    },
    {
        title: "登录来源",
        key: "loginSource",
        sorter: "loginSource",
        width: 150
    },
    {
        title: "登出时间",
        key: "loginOutTime",
        sorter: "loginOutTime",
        width: 150
    },
    // {
    //     title: "分钟",
    //     key: "loginDuration",
    //     sorter: "loginDuration",
    //     width: 150
    // },
    // {
    //     title: "登录时sesstion",
    //     key: "sessionKey",
    //     sorter: "sessionKey",
    //     width: 150
    // },

    {
        title: "创建时间",
        key: "createTime",
        width: 160,
        sorter: "createTime",
        searchProps: {
            type: 'date-range',
          },
    },
    // {
    //     title: "更新时间",
    //     key: "updateTime",
    //     width: 160,
    //     sorter: "updateTime"
    // },

    // {
    //     title: "操作",
    //     key: "id",
    //     type: "id",
    //     fixed: "right",
    //     width: 160,
    //     render: function (row: any) {
    //         return h(NSpace, [
    //             h(NButton, {
    //                 onVnodeMounted: async (vNode: any) => {
    //                     //await checkAuthShow(vNode.el as any, "1P76CZFAXE");
    //                 },
    //                 type: "primary",
    //                 text: true,
    //                 onClick: (async (row: any) => {
    //                     await doUpdateModal(row);
    //                 }).bind(null, row)
    //             }, "编辑"),
    //             h(NButton, {
    //                 onVnodeMounted: async (vNode: any) => {
    //                     // await checkAuthShow(vNode.el as any, "HT4UR87SIB");
    //                 },
    //                 type: "primary",
    //                 text: true,
    //                 onClick: (async (row: any) => {
    //                     await doDeleteModal(row);
    //                 }).bind(null, row)
    //             }, "删除")
    //         ]);
    //     }
    // }
];
/**
 * 新增和更新校验规则
 */
export const formRules = reactive({
        loginName: [{required: true, message: "请输入登录名", trigger: "blur"}],
        loginIp: [{required: true, message: "请输入登录ip", trigger: "blur"}],
        loginMsg: [{required: true, message: "请输入登录消息", trigger: "blur"}],
        fkUserId: [{required: true, message: "请输入用户编号", trigger: "blur"}],
        loginBrowser: [{required: true, message: "请输入浏览器", trigger: "blur"}],
        loginOs: [{required: true, message: "请输入操作系统", trigger: "blur"}],
        loginSource: [{required: true, message: "请输入登录来源", trigger: "blur"}],
        loginOutTime: [{required: true, message: "请输入登出时间", trigger: "blur"}],
        loginDuration: [{required: true, message: "请输入分钟", trigger: "blur"}],
        sessionKey: [{required: true, message: "请输入登录时sesstion", trigger: "blur"}],
    })
;

